/*
 * 
 * LegendShop 多用户商城系统
 * 
 *  版权所有,并保留所有权利。
 * 
 */
package com.legendshop.oa.dao.impl;

import com.legendshop.dao.impl.GenericDaoImpl;
import com.legendshop.dao.support.CriteriaQuery;
import com.legendshop.dao.support.EntityCriterion;
import com.legendshop.dao.support.PageSupport;
import com.legendshop.dao.support.lambda.LambdaEntityCriterion;
import com.legendshop.framework.compare.DataListComparer;
import com.legendshop.oa.comparator.ProjectMemberComparator;
import com.legendshop.oa.comparator.ProjectModuleComparator;
import com.legendshop.oa.dao.ProjectModuleDao;
import com.legendshop.oa.model.ConstTable;
import com.legendshop.oa.model.ProjectMember;
import com.legendshop.oa.model.ProjectModule;

import java.util.List;
import java.util.Map;

/**
 * The Class ProjectModuleDaoImpl. Dao实现类
 */

public class ProjectModuleDaoImpl extends GenericDaoImpl<ProjectModule, Long> implements ProjectModuleDao {

	/**
	 * 根据Id获取
	 */
	public ProjectModule getProjectModule(Long id) {
		return getById(id);
	}

	/**
	 * 删除
	 */
	public int deleteProjectModule(ProjectModule projectModule) {
		return delete(projectModule);
	}

	/**
	 * 保存
	 */
	public Long saveProjectModule(ProjectModule projectModule) {
		return save(projectModule);
	}

	/**
	 * 更新
	 */
	public int updateProjectModule(ProjectModule projectModule) {
		return update(projectModule);
	}

	/**
	 * 查询列表
	 */
	public PageSupport getProjectModule(CriteriaQuery cq) {
		return queryPage(cq);
	}

	@Override
	public PageSupport<ProjectModule> getProjectModuleByProject(Long projectId, String name,String curPage) {
		CriteriaQuery query = new CriteriaQuery(ProjectModule.class, curPage);
		query.setPageSize(20);
		query.eq("projectId", projectId);
		query.like("name", name);
		return queryPage(query);
	}

	@Override
	public List<ProjectModule> getProjectModuleByPro(Long projectId) {
		return this.queryByProperties(new EntityCriterion().eq("projectId", projectId));
	}

	@Override
	public void saveProjectModule(Long projectId, List<ProjectModule> list) {
		DataListComparer<ProjectModule, ProjectModule> comparer = new DataListComparer<>(new ProjectModuleComparator());

		List<ProjectModule> dbList = this.queryByProperties(new LambdaEntityCriterion<ProjectModule>(ProjectModule.class).eq(ProjectModule::getProjectId, projectId));
		Map<Integer, List<ProjectModule>> compareResult = comparer.compare(list,dbList, null);

		//不会有update 列表，直接更新
		List<ProjectModule> delList = compareResult.get(-1);
		List<ProjectModule> addList = compareResult.get(1);
		delete(delList);
		save(addList);

	}

}
